package de.tabuma.pm.project.view;

import de.tabuma.pm.project.entity.Project;
import de.tabuma.lib.*;
import de.tabuma.pm.*;
import de.tabuma.customer.entity.*;
import java.io.*;
import java.util.*;
import java.util.logging.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author alefen
 */
public class list extends tabumaview {


    public list(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }

    @Override
    public void processRequest(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {

        List<Project> pList = (List<Project>) request.getAttribute(constants.listAttribute);
        List<Customer> cList = (List<Customer>) request.getAttribute(constants.customerList);

        String customerListName = "Customer Name";
        String projectListRemove = "remove";
        String projectListName = "Project Name";

        String projectListNoEntry = "No Entry";

        String projectListAdd = "Add";

        out.println("<div class=\"contentContent\">");
        out.println("<table class=\"table\">");
        out.println("<tr>");
        out.println("<td class=\"tableheader\">"+projectListName+"</td>");
        out.println("<td class=\"tableheader\">"+customerListName+"</td>");
        out.println("<td class=\"tableheader\"></td>");
        out.println("</tr>");

        String link1 = "";
        String link2 = "";
        Enumeration enu = this.getParameter().keys();
        boolean first = true;
        while(enu.hasMoreElements()) {
            String key = (String) enu.nextElement();
            String value = request.getParameter(key);
            if(first) {
               link1 += "&amp;";
            }
            link1 += key+"="+value;
            link2 += "<input type=\"hidden\" name=\""+key+"\" value=\""+value+"\"/>\n";
        }

        for(int i=0;i<pList.size();i++) {
            Project project = pList.get(i);
            String pName = project.getName();
            String cName = "";

            for(int j=0;j<cList.size();j++) {
                if(project.getCustomerId()==cList.get(j).getId()) {
                    cName = cList.get(j).getName();
                }
            }

            out.println("<tr>");
            out.println("<td class=\"tablecontent\"><a href=\""+this.getServlet()+"?"+link1+"&amp;"+constants.paramAction+"="+constants.ACTION_OVERVIEW+"&amp;"+constants.projectId+"="+project.getId()+"\">"+pName+"</a></td>");
            out.println("<td class=\"tablecontent\"><a href=\""+this.getServlet()+"?"+link1+"&amp;"+constants.paramAction+"="+constants.ACTION_OVERVIEW+"&amp;"+constants.projectId+"="+project.getId()+"\">"+cName+"</a></td>");
            out.println("<td class=\"tablecontent\"><a href=\""+this.getServlet()+"?"+link1+"&amp;"+constants.paramAction+"="+constants.ACTION_REMOVE+"&amp;"+constants.projectId+"="+project.getId()+"\">"+projectListRemove+"</a></td>");
            out.println("</tr>");
        }
        if(cList.size()==0) {
            out.println("<tr>");
            out.println("<td class=\"tablecontent\" colspan=\"3\">"+projectListNoEntry+"</td>");
            out.println("</tr>");
        }
        out.println("</table>");
        out.println("<br/>");
        out.println("<form action=\""+this.getServlet()+"\">");

        out.println(link2);
        out.println("<input type=\"hidden\" name=\""+constants.paramAction+"\" value=\""+constants.ACTION_CREATE+"\"/>");
        out.println("<input type=\"submit\" value=\"" + projectListAdd + "\" class=\"button\">");
        out.println("</form>");
        out.println("</div>");

    }
    


}
